package org.csu.laboratoryequipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.util.ArrayIterator;
import org.csu.laboratoryequipment.common.CommonResponse;
import org.csu.laboratoryequipment.entity.CompKeyWord;
import org.csu.laboratoryequipment.entity.Result;
import org.csu.laboratoryequipment.model.*;
import org.csu.laboratoryequipment.persistence.ResultMapper;
import org.csu.laboratoryequipment.service.ResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutionException;

@Service("ResultService")
public class ResultServiceImpl implements ResultService {

    private static final int SEARCH_NUM = 100000;

    @Autowired
    private ResultMapper resultMapper;
    @Override
    public CommonResponse<List<Result>> getResult() {
        return CommonResponse.createForSuccessMessage("SUCCESS",resultMapper.selectList(null));
    }

    @Override
    public List<CompKeyWord> getCompKeyAndComNumBySeedWord(String seedWord) throws Exception {

        long start = System.currentTimeMillis();
        DataPreprocessing dataPreprocessing = new DataPreprocessing();
        WordSegment wordSegment = new WordSegment();
        CompKeyAlgorithm compKeyAlgorithm = new CompKeyAlgorithm();

        //确定种子关键词
        ArrayList<String> seedWords = new ArrayList<>();
        seedWords.add(seedWord);

//        //数据清洗,搜索量写死
//        dataPreprocessing.data_clean(SEARCH_NUM);
//
//        //进行多种分词操作,得到分词结果
//        wordSegment.segment_result(FilePath.TRANSFER_FILE_NAME);

        //进行关键字检索
        SeedKeySearch seedKeySearch = new SeedKeySearch();
        seedKeySearch.seedkeysearh(seedWords);

        List<CompKeyWord> result = new ArrayList<>();
        for (int i =0 ;i<seedWords.size();i++)
        {
            System.out.println(seedWords.get(i));
            result = compKeyAlgorithm.comKeyAlgorithm(seedWords.get(i));
        }

        Iterator<CompKeyWord> iterator = result.listIterator();
        while (iterator.hasNext()){
            CompKeyWord flag  = iterator.next();
            System.out.println(flag.getCom_word());
            System.out.println(flag.getCom_num());
        }

        return result;
    }

    @Override
    public void addRecord(String seedWord, List<CompKeyWord> result) {
          Iterator<CompKeyWord> iterator = result.listIterator();
          while (iterator.hasNext()){
              CompKeyWord flag = iterator.next();
              Result temp  = new Result();
              Date currentDate = new Date();
              temp.setComWord(flag.getCom_word());
              temp.setComNum(flag.getCom_num());
              temp.setSeedKey(seedWord);
              temp.setTime(currentDate);
              resultMapper.insert(temp);

          }

    }

    @Override
    public List<Result> searchByseedWord(String seedWord) {
        QueryWrapper<Result> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("seed_key",seedWord);
        return resultMapper.selectList(queryWrapper);
    }
}
